package com.jcompressor.resources;

/*
public class JcompressorResources extends BaseResources {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(JcompressorResources.class);
	// private static final Map<String, Template> TEMPLATES = new LinkedHashMap<String, Template>();
	// private static final Map<String, Resource> RESOURCES = new LinkedHashMap<String, Resource>();
	
	private HttpServletRequest request;
	private Map<String, Map<String, LinkedList<InputStream>>> resources;
	
	public boolean addTemplate() {
			AssertArgument.isNotNull(this.request, "request");
			final Document document = DomUtils.parse(Resources.getResourceFilePath());
			
			final String templateId = this.createTemplateId(this.request.getRequestURI());
			final Element rootElement = document.getRootElement();
			final Element templatesElement = rootElement.getChild(JcompressorConstants.NODE_TEMPLATES);
			
			final Template template = new Template(templateId, this.request.getRequestURI());
			final Element templateElement = this.createTemplateElement(template);
			templatesElement.addContent(templateElement);
			DomUtils.write(document, Resources.getResourceFilePath());
			Resources.TEMPLATES.put(templateId, template);
			
			final List<Object> templates = rootElement.getChildren("template");
			for (final Object templateObj : templates) {
				final Element templateElement = (Element)templateObj;
				if (StringUtils.equals("template", templateElement.getName())) {
					final String uri = templateElement.getAttributeValue("uri");
					if (StringUtils.equals(this.getContext().getUri(), uri)) {
						final Element resourcesElement = templateElement.getChild("resources");
						final List<Object> resources = resourcesElement.getChildren("resource");
						if (resources.size() == 0) {
							final Element resourceElement = new Element("resource");
							final Element fileElement = new Element("file");
							fileElement.setText(this.getOutputFile().getName());
							resourceElement.addContent(fileElement);
							
							final Element checksumElement = new Element("checksum");
							checksumElement.setText(checksum);
							resourceElement.addContent(checksumElement);
							
							resourcesElement.addContent(resourceElement);
						}
						else {
							Boolean clean = Boolean.TRUE;
							for (final Object resourceObj : resources) {
								final Element resourceElement = (Element)resourceObj;
								if (StringUtils.equals(checksum, resourceElement.getChildText("checksum"))) {
									System.out.println(checksum);
									clean = Boolean.FALSE;
								}
							}
							
							if (clean) {
								resources.clear();
							}
						}
					}
				}
			}
			
		return true;
	}
	
	public boolean addResource() {
		AssertArgument.isNotNull(this.request, "request");
		final Document document = DomUtils.parse(Resources.getResourceFilePath());
		final String templateId = this.createTemplateId(this.request.getRequestURI());
		
		// `
		// JcompressorResources.RESOURCES.put(templateId, template);
		
		final Element rootElement = document.getRootElement();
		final Element resourcesElement = rootElement.getChild(JcompressorConstants.NODE_RESOURCES);
		
		// final Template template = new Template(templateId, this.request.getRequestURI());
		//final Element resource = this.createResourceElement(resourcesElement, this.request.getRequestURI());
		DomUtils.write(document, Resources.getResourceFilePath());
		//JcompressorResources.ELEMENTS.put(templateId, resource);
		
		return true;
	}
	
	public boolean updateTemplate() {
		AssertArgument.isNotNull(this.request, "request");
		final String templateId = this.createTemplateId(this.request.getRequestURI());
		// final Element rootElement = this.document.getRootElement();
		// final Element templatesElement = rootElement.getChild(JcompressorConstants.NODE_TEMPLATES);
		final Template template = Resources.TEMPLATES.get(templateId);
		template.setUpdated(ResourceUtils.generateUnixDateTime());
		
		// DomUtils.write(this.document, JcompressorResources.getResourceFilePath());
		Resources.TEMPLATES.put(templateId, template);
		return true;
	}
	
	public String createTemplateId(final String uri) {
		String templateId = RegexUtils.replaceAll(uri, RegexUtils.TEMPLATE_ID_PHASE1, StringUtils.EMPTY);
		templateId = RegexUtils.replaceAll(templateId, RegexUtils.TEMPLATE_ID_PHASE2, "-");
		return templateId;
	}
	
	public Element createTemplateElement(final Template template) {
		final Element element = new Element(JcompressorConstants.NODE_TEMPLATES);
		element.setAttribute("id", template.getId());
		element.setAttribute("uri", template.getUri());
		element.setAttribute("updated", Long.toString(template.getUpdated()));
		LOGGER.info("Creating Template Element " + template);
		return element;
	}
	
	public static void execute(final Runnable runnable) {
		JcompressorResources.execute(new Runnable[] {runnable});
	}
	
	public static void execute(final Runnable[] runnables) {
		ExecutorService executor = null;
		
		try {
			executor = Executors.newFixedThreadPool(runnables.length);
			for (final Runnable runnable : runnables) {
				final Future<?> process = executor.submit(runnable);
				if (process != null) {
					process.get();
				}
			}
		}
		catch (InterruptedException e) {
			throw new JcompressorException("Compression thread was interrupted", e);
		}
		catch (ExecutionException e) {
			throw new JcompressorException("Computation error", e);
		}
		finally {
			if (executor != null) {
				executor.shutdown();
			}
		}
	}
	
	public static ClassLoader getResourceLoader() {
		final ClassLoader resourceLoader = Thread.currentThread().getContextClassLoader();
		if (resourceLoader == null) {
			throw new JcompressorException("Resource Loader is null");
		}
		
		return resourceLoader;
	}
	
	public void setRequest(final HttpServletRequest request) {
		this.request = request;
	}
	
	public void setResources(final Map<String, Map<String, LinkedList<InputStream>>> resources) {
		LOGGER.debug(resources.toString());
		this.resources = resources;
	}
	
	public Boolean hasTemplate() {
		AssertArgument.isNotNull(this.request, "request");
		return this.hasTemplate(this.createTemplateId(this.request.getRequestURI()));
	}
	
	public Boolean hasTemplate(final String templateId) {
		return Resources.TEMPLATES.containsKey(templateId);
	}
	
}
*/